/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parsers;

import java.io.File;
import java.util.List;
import java.util.ListIterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.xml.sax.Attributes;

import org.xml.sax.helpers.XMLFilterImpl;

/**
 *
 * @author rejun001
 */
/**
 * This shows how to parse an XML File using SAX and display the same
 * information from the xml file by implementing the ContentHandler interface
 */
public class UsingJAXPTest1 {

    public static void main(String args[]) {

        try {
            JAXBContext jc = JAXBContext.newInstance("parsers");
            Unmarshaller um = jc.createUnmarshaller();
            Collection collection = (Collection) um.unmarshal(new File("books.xml"));

            Collection.Books books = collection.getBooks();
            List<BookType> bookList = books.getBook();

            ListIterator<BookType> it = bookList.listIterator();
            while (it.hasNext()) {
                BookType book = it.next();
                System.out.print("Book: Name = '" + book.getName().trim()
                        + "', Category = " + book.getBookCategory().trim() 
                        + "', ISBN = " + book.getISBN() 
                        + ", price = " + book.getPrice().trim());
                System.out.print(", ItemID = " + book.getItemId().trim() 
                        + ", pubdate = " + book.getPublicationDate().toString() 
                        + ", ");
                ListIterator<String> ait = book.getAuthors().getAuthorName().listIterator();
                while (ait.hasNext()) {
                    System.out.print("Author: " + ait.next().trim() + ", ");
                }

                System.out.println("");
                System.out.println("\tDesc: " + book.getDescription().trim());
                
                // make a change on the Java side
                if (book.getName().contains("War")) {
                    book.setName("Voina i Mir");
                }
            }

            Marshaller m = jc.createMarshaller();
            m.marshal(collection, System.out);

        } catch (Exception je) {
            je.printStackTrace();
        }


//        FileInputStream instream = null;
//        InputSource is=null;
//	try {
//	    SAXParserFactory spf = SAXParserFactory.newInstance();
//	    spf.setNamespaceAware(true);
//	    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
//
//	    xmlReader.setContentHandler(new MyContentHandler());
//
//            instream = new FileInputStream("books.xml");
//            is = new InputSource(instream);
//
//	    xmlReader.parse(is);
//	} catch( IOException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	} catch( SAXException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	} catch( ParserConfigurationException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	}

    }
}

class MyContentHandler extends XMLFilterImpl {

    boolean printMe = false;
    static int authorCount = 0;

    private boolean getPrintValue() {
        return printMe;
    }

    private void setPrintValue(boolean printValue) {
        printMe = printValue;
    }

    public void characters(char[] ch, int start, int length) {
        String gotString = new String(ch, start, length);
        if (printMe) {
            if (!gotString.trim().equals("")) {
                System.out.println(gotString.trim());
            }
        }
    }

    public void startElement(String namespaceURI, String localName,
            String qName, Attributes attributes) {

        if (localName.equals("book")) {
            authorCount = 0;
            System.out.println("\nBook Details");
            int attrsLength = attributes.getLength();
            for (int i = 0; i < attrsLength; i++) {
                String itemId = attributes.getValue(i);
                System.out.println("ItemId " + itemId);

            }
        }

        if (localName.equals("ISBN")
                || localName.equals("price")
                || localName.equals("description")
                || localName.equals("Discount")
                || localName.equals("bookCategory")
                || localName.equals("name")) {

            System.out.println("Book " + localName + ": ");
            printMe = true;

        } else if (localName.equals("authorName")) {
            System.out.println("Book " + localName + ": ");
            authorCount++;
            printMe = true;

        } else {
            printMe = false;
        }


    }

    public void endElement(String namespaceURI, String localName, String qName) {
        if (localName.equals("authors")) {
            System.out.println("No of authors :" + authorCount);
        }
    }
}
